define(['vue','layer'],function (Vue,layer){
    'use strict';
    const muimg = {
        template:`<el-upload
                                class="avatar-uploader"
                                :action="uploadurl"
                                :show-file-list="false"
                                :on-success="(response,uploadFile)=>handleFileSuccess(response,uploadFile)"
                        >
                            <img v-if="modelValue" :src="modelValue" class="avatar" />
                            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                        </el-upload><el-button style="margin-left: 20px" color="#626aef" type="primary" @click="handleShowAttachment">选择图片</el-button>`,
        props:{
            modelValue: { type: String, default: '' },
        },
        emits: ['update:modelValue'],
        data(){
            return {
                uploadurl: Config.upload.uploadurl
            }
        },
        setup(props, { emit }) {

            // console.log(Config,'----------------------')
            const handleFileSuccess = (response,uploadFile,name)=>{
                    if(response.code==1){
                        emit('update:modelValue',response.data.url)
                    }
            }
            const handleShowAttachment = (multiple=0)=>{
                let url = Config.domain+Config.attachment_url+`?select=1&multiple=${multiple}`
                let option = {
                    callback:function (res){
                        // console.log(res)
                        emit('update:modelValue',res)
                    }
                }
                if(option.callback){
                    window.callback = option.callback
                }
                if(option.success){
                    window.success = option.success
                }
                if(option.cancel){
                    window.cancel = option.cancel
                }
                if(option.end){
                    window.end = option.end
                }
                if(option.close){
                    window.close = option.close
                }
                if(url.indexOf('http')==-1){
                    url = Config.moduleurl+'/'+url;
                }
                option.title = option.title||'页面'
                option.shadeClose = option.shadeClose||true
                option.shade = option.shade||0.8
                option.area = option.area||['99%','99%']
                option.success = option.success||null
                option.cancel = option.cancel||null
                option.end = option.end||null
                option.close = option.close||null
                option.content = url
                option.type = option.type||2
                layer.open(option)
            }
            return {
                handleFileSuccess,
                handleShowAttachment
            }
        }
    }
    return muimg
})